<?php	
	
	$user = $controller->getCurrentUser();
	
	if ($user->getGrade()->getType() == GRADE_TYPE_ADMIN)
	{
		$showEditView = true;
			
		$userId = intval($request->getParam('id'));
		
		if ($userId)
		{
			if ($userId != 0)
			{
				$userToChange = user::singleton($userId);
				
				if ($request->getMethod() == 'POST')
				{
					if ($request->getParam('form') == 'data')
					{
						try
						{
							$login = addslashes($request->getParam('login'));
							$lastname = addslashes($request->getParam('lastname'));
							$firstname = addslashes($request->getParam('firstname'));
							$email = $request->getParam('email');
							$semester = intval($request->getParam('semester'));
							$gradeId = intval($request->getParam('grade'));
							$comment = addslashes($request->getParam('comment'));
							$paid = $request->getParam('paid') ? true : false;
							$deposit = $request->getParam('deposit') ? true : false;
							$saveRecords = $request->getParam('saveRecords') ? true : false;
							
							$userToChange->setRoles($request->getParam('role'));
							$userToChange->setLogin($login);
							$userToChange->setLastname($lastname);
							$userToChange->setFirstname($firstname);
							$userToChange->setEmail($email);
							$userToChange->setSemester($semester);
							$userToChange->setGrade($gradeId);
							$userToChange->setComment($comment);
							$userToChange->setPaid($paid);
							$userToChange->setDeposit($deposit);
							$userToChange->setSaveRecords($saveRecords);
							$userToChange->update();
							
							logPermactions($user->getId(),'Utilisateur modifi&eacute; : '.$userToChange->getCompleteName());
							
							$controller->addInformation('Modifications effectu&eacute;es.');
							$showEditView = false;
						}
						catch (Exception $e)
						{
							$e->_log();
							$controller->addError('Erreur : Les modifications n\'ont pas pu &ecirc;tre effectu&eacute;es.');
						}
					}
					else
					{
						try 
						{
							$password = addslashes($request->getParam('password'));
							$passwordconf = addslashes($request->getParam('passwordconf'));
							
							if ($password == $passwordconf)
							{	
								$userToChange->changePassword($password);
								$controller->addInformation('Mot de passe modifi&eacute;.');
								$showEditView = false;
							}
							else
							{
								$controller->addError('Erreur : les deux mots de passes ne sont pas identiques.');
							}
						}
						catch (Exception $e)
						{
							$e->_log();
							$controller->addError('Erreur : Le mot de passe n\'a pas pu &ecirc;tre modifi&eacute;.');
						}
					}
				}
			}
		}
		else
		{
			$showEditView = false;
		}
		
		// Affichage de la vue
		$controller->setVar('action', $action);
		
		if ($showEditView)
		{
			$controller->setVar('gradeList',grade::getGradesList(true));
			$controller->setVar('roleList',grade::getRolesDrupal());
			$controller->setVar('userToChange', $userToChange);
			$controller->setVar('user', $user);
			$controller->render("editUserAdmin");
		}
		else
		{
			$controller->setVar('userList', user::getUserListByGradeType(array(1, 2, 3)));
			$controller->setVar('user',$user);
			$controller->render('searchUser');
		}
	}
	else
		$controller->render('errorRights');
?>